<template>
  <el-dialog title="添加测评对象" :visible.sync="isAddProgress" width="80%" :before-close="handleClose" :close-on-click-modal='false'>
    <el-tabs v-model="activeName">
      <el-tab-pane label="按层级批量添加" name="hierarchy">
        <el-tree :data="hierarchyList" show-checkbox node-key="web_id" :props="defaultProps" @check="getCheckedNodes"> </el-tree>
      </el-tab-pane>
      <el-tab-pane label="添加单个学生" name="single">
        <el-row :gutter="20">
          <el-col :span="4"><el-input v-model="form.real_name" placeholder="请输入学生姓名" size="small"></el-input></el-col>
          <el-col :span="2"><el-button type="primary" size="small" @click="searchFn">搜索</el-button></el-col>
        </el-row>
        <br>
        <el-row :gutter="22" class="centerTable" style="width:99%;margin:0 auto;">
          <simple-table :tableConfig="tableConfig" :tableData="tableData" @handleSelectionChange="goToDeleteSelect"></simple-table>
        </el-row>
      </el-tab-pane>
    </el-tabs>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="subimFn">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import simpleTable from '@/components/table.vue'
import {APIHIERARCHY, APISCHOOLADDEVALUATIONOBJECT, APISTUDENTLIST} from '@/common/api.js'
export default {
  components: {simpleTable},
  props: ['isAddProgress', 'id', 'schoolId'],
  data () {
    return {
      activeName: 'hierarchy',
      hierarchyList: [],
      defaultProps: {
        value: 'id',
        children: 'children',
        label: 'title'
      },
      form: {
        real_name: '',
        exportExcel: 1
      },
      tableConfig: {
        load: false,
        size: 'small', // 表格大小
        style: 'width:100%', // 表格宽度
        align: 'center',
        selection: true,
        border: true,
        config: [
          {name: '序号', type: 'index', width: '60px'},
          {id: 'real_name', name: '学生姓名'},
          {id: 'sex_name', name: '性别'},
          {id: 't_id', name: '账号'},
          {id: 'student_id', name: '学籍号'},
          {id: 'grade_title', name: '年级'},
          {id: 'class_title', name: '班级'},
          {id: 'birthday', name: '生日'},
          {id: 'flag_name', name: '状态'}
        ]
      },
      tableData: [],
      checkStr: []
    }
  },
  created () {
    this.hierarchyFn()
    this.tableDataFn()
  },
  methods: {
    hierarchyFn () {
      APIHIERARCHY({
        school_id: this.schoolId
      }).then((res) => {
        this.hierarchyList = res.data
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    searchFn () {
      this.tableDataFn()
    },
    tableDataFn () {
      this.form.school_id = this.schoolId
      APISTUDENTLIST(this.form).then((res) => {
        if (res.status === 1) {
          this.tableData = res.data.list
        } else {
          this.$message.error(res.msg)
          this.InIt()
        }
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    goToDeleteSelect (val) { // 多选
      let arr = []
      val.val.forEach(item => {
        arr.push(item.uid)
      })
      this.checkStr = arr
    },
    getCheckedNodes (data, checked, indeterminate) {
      let arr = []
      checked.checkedNodes.forEach(item => {
        if (item.level === '班级') {
          arr.push(item.id)
        }
      })
      this.checkStr = arr
    },
    handleClose (type) {
      this.$emit('noAddProgress', type)
    },
    subimFn () {
      APISCHOOLADDEVALUATIONOBJECT({
        plan_id: this.id,
        school_id: this.schoolId,
        type: this.activeName === 'hierarchy' ? 1 : 2,
        object: this.checkStr
      }).then((res) => {
        this.$message.success(res.msg)
        this.handleClose(1)
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    }
  }
}
</script>
